package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.*;
import com.ysd.lis.mapper.lab.LabItemDictMapper;
import com.ysd.lis.mapper.lab.LabMapperItemMapper;
import com.ysd.lis.request.ItemMapperDto;
import com.ysd.lis.request.RptGroupPageParam;
import com.ysd.lis.service.lab.LabMapperItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 项目和项目之间的关联关系表(合并标本用) 服务实现类
 * </p>
 *
 * @author wanghuaixing
 * @since 2024-12-27
 */
@Service
public class LabMapperItemServiceImpl extends ServiceImpl<LabMapperItemMapper, LabMapperItem> implements LabMapperItemService {
    @Autowired
    LabItemDictMapper labItemDictMapper;

    @Autowired
    LabMapperItemMapper labMapperItemMapper;


    @Override
    public Result findMapperItem(LabItemDict labItemDict) {
        LambdaQueryWrapper<LabMapperItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LabMapperItem::getRptGroup, labItemDict.getRptGroup())
                .eq(LabMapperItem::getItemNo, labItemDict.getItemNo())
                .eq(LabMapperItem::getDelFlag, 0);

        List<LabMapperItem> labMapperItems = labMapperItemMapper.selectList(queryWrapper);

        // MPJLambdaWrapper<LabItemDict> queryWrapper = new MPJLambdaWrapper<>();
        // queryWrapper.selectAll(LabItemDict.class);
        // queryWrapper.selectAs(LabMapperItem::getMapperItemCode, LabItemDict::getMapperItemCode)
        //         .selectAs(LabMapperItem::getMapperItemName, LabItemDict::getMapperItemName);
        //
        // queryWrapper.innerJoin(LabMapperItem.class, p -> p.eq(LabMapperItem::getItemNo, LabItemDict::getItemNo).eq(LabItemDict::getRptGroup, LabMapperItem::getRptGroup).eq(LabItemDict::getDelFlag, 0));
        // queryWrapper.eq(LabItemDict::getRptGroup, labItemDict.getRptGroup())
        //         .eq(LabItemDict::getItemNo, labItemDict.getItemNo());
        // List<LabItemDict> labItemDicts = labItemDictMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功！", labMapperItems);
    }

    @Override
    public Result findItemsByRptGroup(RptGroupPageParam param) {

        // 非空检查
        if (param.getRptGroupCode() == null || param.getItemNo() == null) {
            throw new IllegalArgumentException("参数不能为空");
        }

        // 构建子查询
        LambdaQueryWrapper<LabMapperItem> subQueryWrapper = new LambdaQueryWrapper<>();
        subQueryWrapper.eq(LabMapperItem::getItemNo, param.getItemNo())
                .eq(LabMapperItem::getDelFlag, 0);

        List<LabMapperItem> labMapperItems = labMapperItemMapper.selectList(subQueryWrapper);

        //取出labMapperItems中的mapperItemCode
        List<String> itemNoList = labMapperItems.stream()
                .map(LabMapperItem::getMapperItemCode)
                .collect(Collectors.toList());

        // 构建主查询
        LambdaQueryWrapper<LabItemDict> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //判断itemNoList 是否为空
        if (itemNoList.size()>0) {
            lambdaQueryWrapper.eq(LabItemDict::getDelFlag, 0)
                    .eq(LabItemDict::getRptGroup, param.getRptGroupCode())
                    .ne(LabItemDict::getItemNo, param.getItemNo())
                    .notIn(LabItemDict::getItemNo, itemNoList);
        }else {
            lambdaQueryWrapper.eq(LabItemDict::getDelFlag, 0)
                    .eq(LabItemDict::getRptGroup, param.getRptGroupCode())
                    .ne(LabItemDict::getItemNo, param.getItemNo());
        }

        if (StringUtils.isNotBlank(param.getKeyWords())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(LabItemDict::getItemNo, param.getKeyWords())
                    .or().like(LabItemDict::getItemNa, param.getKeyWords()));
        }
        List<LabItemDict> labItemDicts = labItemDictMapper.selectList(lambdaQueryWrapper);
        List<LabItemDict> collect1 = labItemDicts.stream().sorted(Comparator.comparing(LabItemDict::getSeq, Comparator.nullsLast(Integer::compareTo))).collect(Collectors.toList());

        return Result.succ(1, "查询成功", collect1);
    }

    @Override
    public Result batchMapperItemList(ItemMapperDto dto) {
        List<LabItemDict> addItemList = dto.getAddItemList();
        LabItemDict sourceItem = dto.getSourceItem();

        if (addItemList != null && addItemList.size() > 0) {
            List<LabMapperItem> list = new ArrayList<>();
            for (LabItemDict item : addItemList) {
                LabMapperItem labMapperItem = new LabMapperItem();
                labMapperItem.setPid(sourceItem.getId());
                labMapperItem.setCid(item.getId());
                labMapperItem.setItemNo(sourceItem.getItemNo());
                labMapperItem.setItemNa(sourceItem.getItemNa());
                labMapperItem.setMapperItemCode(item.getItemNo());
                labMapperItem.setMapperItemName(item.getItemNa());
                labMapperItem.setRptGroup(sourceItem.getRptGroup());
                list.add(labMapperItem);
            }
            boolean b = this.saveBatch(list);

            for (LabItemDict item : addItemList) {
                LabMapperItem labMapperItem = new LabMapperItem();
                labMapperItem.setPid(item.getId());
                labMapperItem.setCid(sourceItem.getId());
                labMapperItem.setItemNo(item.getItemNo());
                labMapperItem.setItemNa(item.getItemNa());
                labMapperItem.setMapperItemCode(sourceItem.getItemNo());
                labMapperItem.setMapperItemName(sourceItem.getItemNa());
                labMapperItem.setRptGroup(item.getRptGroup());
                boolean b1 = this.saveOrUpdate(labMapperItem);
            }
        }

        return Result.succ(1, "成功", "");

    }

    @Override
    public Result batchDeleteMapperItem(ItemMapperDto dto) {
        List<LabMapperItem> labMergerRelationshipList = dto.getLabMergerRelationshipList();
        //取出id
        List<String> ids = labMergerRelationshipList.stream().map(LabMapperItem::getId).collect(Collectors.toList());
        for (LabMapperItem item : labMergerRelationshipList) {
            LambdaQueryWrapper<LabMapperItem> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(LabMapperItem::getMapperItemCode, item.getItemNo())
                    .eq(LabMapperItem::getItemNo, item.getMapperItemCode())
                    .eq(LabMapperItem::getRptGroup, item.getRptGroup())
                    .eq(LabMapperItem::getDelFlag, 0);
            this.remove(queryWrapper);
        }
        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ("删除成功");
        } else {
            return Result.fail("删除失败");
        }

    }
}
